package org.springside.examples.showcase.web.vacation;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.examples.showcase.entity.User;
import org.springside.examples.showcase.entity.Vacation;
import org.springside.examples.showcase.service.ShiroDbRealm.ShiroUser;
import org.springside.examples.showcase.service.vacation.VacationService;

import com.google.common.collect.Maps;

@Controller
@RequestMapping(value = "/vacation")
public class VacationController {
	
	Logger log = LoggerFactory.getLogger(VacationController.class);
	
	@Autowired
	private VacationService vacationService;

	private static final int PAGE_SIZE = 3;

	private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
	static {
		sortTypes.put("auto", "自动");
		//sortTypes.put("title", "标题");
	}

	@RequestMapping(value = "")
	public String list(@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model) {
		log.info("vacation list...");
		Long userId = getCurrentUserId();
		Page<Vacation> vacations = vacationService.pageByUserId(userId, pageNumber, PAGE_SIZE, sortType);
		model.addAttribute("vacations", vacations);

		model.addAttribute("sortType", sortType);
		model.addAttribute("sortTypes", sortTypes);

		return "vacation/vacationList";
	}

	@RequestMapping(value = "create")
	public String createForm(Model model) {
		model.addAttribute("vacation", new Vacation());
		return "vacation/vacationForm";
	}

	@RequestMapping(value = "save")
	public String create(@Valid Vacation newVacation, RedirectAttributes redirectAttributes) {
		User user = new User(getCurrentUserId());
		newVacation.setUser(user);

		vacationService.saveVacation(newVacation);
		redirectAttributes.addFlashAttribute("message", "创建请假成功");
		return "redirect:/vacation/";
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) throws Exception {
		// 注册自定义的属性编辑器
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		CustomDateEditor dateEditor = new CustomDateEditor(df, true);
		// 表示如果命令对象有Date类型的属性，将使用该属性编辑器进行类型转换
		binder.registerCustomEditor(Date.class, dateEditor);
	}
	
//	@RequestMapping(value = "update/{id}")
//	public String updateForm(@PathVariable("id") Long id, Model model) {
//		model.addAttribute("task", taskService.getTask(id));
//		return "task/taskForm";
//	}
//
//	@RequestMapping(value = "save/{id}")
//	public String update(@Valid Task task, RedirectAttributes redirectAttributes) {
//		taskService.saveTask(task);
//		redirectAttributes.addFlashAttribute("message", "更新任务成功");
//		return "redirect:/task/";
//	}
//
//	@RequestMapping(value = "delete/{id}")
//	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
//		taskService.deleteTask(id);
//		redirectAttributes.addFlashAttribute("message", "删除任务成功");
//		return "redirect:/task/";
//	}

	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}
}
